/**
 * Created by yuris on 10.02.15.
 */
package controller {
import model.PrototypeProxy;
import model.RuntimeProxy;
import model.UserProxy;
import model.objects.ProductWarehouseData;

import model.WarehouseDataProxy;
import model.prototypes.ProductProto;

import mvcexpress.core.ProxyMap;

import mvcexpress.mvc.Command;

public class UnpackWarehouseDataCommand extends Command {
    [Inject]
    public var prototypeProxy:PrototypeProxy;
    [Inject]
    public var userProxy:UserProxy;

    /*
     _userProxy.warehouseDataPacked = {
         size:6,
         volume:10000,
         items:"p1x200d123456789|null||p2x100d123456789"
     }
     */
    public function execute(packedSetting:Object):void {
        //userProxy.warehouseData =
        _unpackWarehouseData(packedSetting);

    }

    private var _bufProductData:ProductWarehouseData;
    private var _bufProductStr:String;
    private function _unpackWarehouseData(packedSetting:Object):void {
        var _warehouseDataProxy:WarehouseDataProxy = userProxy.warehouseDataProxy;

        _warehouseDataProxy.volume = packedSetting.volume;
        var _productsArray:Array = String(packedSetting.items).split("|");
        for (var i:int = 0; i < packedSetting.size; i++) {
            this._bufProductStr = _productsArray[i];
            if(this._bufProductStr != "null" && this._bufProductStr != null && this._bufProductStr != ""){
                this._bufProductData = this._getWarehouseProductByStr(this._bufProductStr);
            }else {
                this._bufProductData = null;
            }
            if(_warehouseDataProxy.size <= i){
                _warehouseDataProxy.addCell();
            }
            _warehouseDataProxy.updateCellData(i,this._bufProductData);
        }

    }

    private var _bufArray:Array;
    private var _deliverTime:uint;
    private var _productProto:ProductProto;
    ///input:  "p1x200d123456789"
    private function _getWarehouseProductByStr(produstStr:String):ProductWarehouseData {
        this._bufArray = produstStr.split("x");
        this._productProto = prototypeProxy.getProductProto(_bufArray[0]);
        this._bufArray = _bufArray[1].split("d");
        this._deliverTime = uint(_bufArray[1]);

        var _result:ProductWarehouseData = new ProductWarehouseData(this._productProto,this._deliverTime);
        _result.count = int(_bufArray[0]);
        return _result;
        //var _result:ProductWarehouseData = new ProductWarehouseData()
    }
}
}
